package com.atguigu.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.Utils;

import jdk.nashorn.internal.ir.RuntimeNode.Request;

/**
 * Servlet implementation class ClientBookServlet
 */
public class ClientBookServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
    BookService bookService = new BookServiceImpl();

	protected void getPage(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int pageNo = Utils.parseInt(request.getParameter("pageNo"), 1);
		int pageSize = Utils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
		Page<Book> page = bookService.page(pageNo, pageSize);
		page.setUrl("client/bookServlet?action=getPage");
		request.setAttribute("page", page);
		request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
	}
	
	protected void getPageByPrice(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		double min = Utils.parseDouble(request.getParameter("min"), 0);
		double max = Utils.parseDouble(request.getParameter("max"), Double.MAX_VALUE);
		
		int pageNo = Utils.parseInt(request.getParameter("pageNo"), 1);
		int pageSize = Utils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
		Page<Book> page = bookService.getPageByPrice(pageNo, pageSize,min,max);
		String url = "client/bookServlet?action=getPageByPrice";
		if(request.getParameter("min") != null) {
			url += "&min=" + request.getParameter("min");
		}
		if(request.getParameter("max") != null) {
			url += "&max=" + request.getParameter("max");
		}
		page.setUrl(url);
		request.setAttribute("page", page);
		request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
	}

}
